'''
第 0010 题：使用 Python 生成类似于下图中的字母验证码图片
'''
from PIL import Image,ImageFont,ImageDraw,ImageFilter
import random


def createLetter():
# 生成字母数字
    letter = []
    for i in range(65, 91):
        letter.append(chr(i))
    for i in range(97, 123):
        letter.append(chr(i))
    for i in range(48, 58):
        letter.append(chr(i))
    return letter


# 随机颜色1:
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 随机颜色2:
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
L = createLetter()
img = Image.new('RGB', (240, 60), (255, 255, 255))
font = ImageFont.truetype("arial.ttf", 36)
draw = ImageDraw.Draw(img)
# 填充像素
for x in range(240):
    for y in range(60):
        draw.point((x, y), fill=rndColor())
# 填写字符
for i in range(0, 4):
    draw.text((60*i+10, 10), L[random.randint(0,len(L)-1)], font=font,fill=rndColor2())
# 模糊图片
img1 = img.filter(ImageFilter.BLUR)
img1.show()
# 扩展
# 添加随机线条